﻿using System;
using System.IO;
using System.Text;
using Autofac;
using NVelocity;
using NVelocity.App;
using uTILLIty.Rave.Core.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace uTILLIty.Rave.Core
{
	[TestClass]
  public class ConfigurationTests : TestBase
	{

		[TestMethod, DeploymentItem("Samples\\SimpleConfiguration.xml")]
		public void Load()
		{
			Contract.Configuration.Configuration target = null;
      using (var configData = File.OpenRead(@"SimpleConfiguration.xml"))
			{
				// ReSharper disable ConditionIsAlwaysTrueOrFalse
				target = target.Load(configData, null);
				// ReSharper restore ConditionIsAlwaysTrueOrFalse
			}

			Assert.AreEqual(@".\Test", target.RootDirectory);
		}

		[TestMethod, DeploymentItem("Samples\\SimpleConfiguration.xml")]
		public void LoadWithValidation()
		{
			Contract.Configuration.Configuration target = null;
			using (var configData = File.OpenRead(@"SimpleConfiguration.xml"))
			{
				// ReSharper disable ConditionIsAlwaysTrueOrFalse
				target = target.Load(configData, GetValidationSchemas());
				// ReSharper restore ConditionIsAlwaysTrueOrFalse
			}

			Assert.AreEqual(@".\Test", target.RootDirectory);
		}

		[TestMethod, DeploymentItem("Samples\\NVelocity.xml")]
		public void VelocitySameDir()
		{
      var vengineFactory = Container.Resolve<Func<string, VelocityEngine>>();
      var vcontext = new VelocityContext();
			vcontext.Put("Now", DateTime.Now);
		  var vengine = vengineFactory.Invoke("");
			var template = vengine.GetTemplate(@"NVelocity.xml", "UTF-8");
			string output;
			using (var writer = new StringWriter())
			{
				template.Merge(vcontext, writer);
				output = writer.GetStringBuilder().ToString();
			}
			TestContext.WriteLine(output);
			Contract.Configuration.Configuration target = null;

			using (var configData = new MemoryStream(output.Length))
			{
				using (var writer = new StreamWriter(configData, Encoding.UTF8))
				{
					writer.Write(output);
					writer.Flush();
					configData.Seek(0, SeekOrigin.Begin);
					// ReSharper disable ConditionIsAlwaysTrueOrFalse
          target = target.Load(configData, GetValidationSchemas());
					// ReSharper restore ConditionIsAlwaysTrueOrFalse
				}
			}
			Assert.AreEqual(@".\Test\" + DateTime.Now.Year, target.RootDirectory);
		}

		[TestMethod]
		public void VelocityAlternateDir()
		{
			var vengineFactory = Container.Resolve<Func<string, VelocityEngine>>();
			var vcontext = new VelocityContext();
			var guid = Guid.NewGuid().ToString();
			vcontext.Put("guid", guid);

			var outFile = new FileInfo(Path.Combine(TestContext.TestDeploymentDir, @"..\..\..\VelocityAlternateDir.vtpl"));
			TestContext.WriteLine(outFile.FullName);
			File.WriteAllText(outFile.FullName, @"Guid=$guid");

			var vengine = vengineFactory.Invoke(outFile.DirectoryName);
			var template = vengine.GetTemplate(outFile.Name, "Windows-1252");

			string output;
			using (var writer = new StringWriter())
			{
				template.Merge(vcontext, writer);
				output = writer.GetStringBuilder().ToString();
			}
			TestContext.WriteLine(output);
			Assert.AreEqual("Guid=" + guid, output);
		}
	}
}
